package com.domian.news.adapters;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.util.LruCache;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.domian.news.R;
import com.domian.news.models.Blog;
import com.domian.news.utils.TimeUtil;
import com.makeramen.roundedimageview.RoundedImageView;

import java.util.List;

/**
 * 博文列表适配器
 * CCreated by hhj on 2016-06-21.on 2016-06-21.
 */
public class BlogListAdapter extends RecyclerView.Adapter<BlogListAdapter.RecyclerViewViewHolder> {

    private Context mContext;
    private List<Blog> mBlogs;

    public BlogListAdapter(Context context, List<Blog> blogs) {
        this.mContext = context;
        this.mBlogs = blogs;
    }

    /**
     * 设置新的数据源，提醒adatper更新
     *
     * @param blogs
     */
    public void refreshData(List<Blog> blogs) {
        this.mBlogs = blogs;
        this.notifyDataSetChanged();
    }


    /**
     * 创建ViewHolder
     *
     * @param viewGroup
     * @param i
     * @return
     */
    @Override
    public RecyclerViewViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_recyclerview_item, viewGroup, false);
        return new RecyclerViewViewHolder(view);
    }

    /**
     * 根据资源ID返回Bitmap对象
     *
     * @param resId
     * @return
     */
    public Bitmap getBitmapFromRes(int resId) {
        Resources res = mContext.getResources();
        return BitmapFactory.decodeResource(res, resId);

    }

    /**
     * 绑定数据
     *
     * @param viewholder
     * @param i
     */
    @Override
    public void onBindViewHolder(RecyclerViewViewHolder viewholder, int i) {
        //设置头像
     /*  if (mBlogs.get(i).getAuthorAvatar() != null && !"".equals(mBlogs.get(i).getAuthorAvatar())) {
            ImageCacheManager.loadImage(mBlogs.get(i).getAuthorAvatar(), viewholder.mUserhead, getBitmapFromRes(R.drawable.photo), getBitmapFromRes(R.drawable.photo));
        } else {
            viewholder.mUserhead.setImageResource(R.drawable.photo);
        }*/

        if (mBlogs.get(i).getAuthorAvatar() != null && !"".equals(mBlogs.get(i).getAuthorAvatar())) {
            imageCacheLoader(mBlogs.get(i).getAuthorAvatar(),mContext,viewholder.mUserhead);
        }else {
            // Log.i("img",mBlogs.get(i).getAuthorAvatar());
            viewholder.mUserhead.setImageResource(R.drawable.photo);
        }
        viewholder.mTitle.setText(mBlogs.get(i).getBlogTitle());
        viewholder.mDescription.setText(mBlogs.get(i).getBlogSummary());
        viewholder.mBest.setText(mBlogs.get(i).getBlogDiggs());
        viewholder.mComment.setText(mBlogs.get(i).getBlogComments());
        viewholder.mBrowse.setText(mBlogs.get(i).getBlogViews());
        //处理日期特殊格式
        String date = TimeUtil.DateToChineseString(TimeUtil.ParseUTCDate(mBlogs.get(i).getBlogPublished()));
        viewholder.mTime.setText(date);
    }

    @Override
    public int getItemCount() {
        return mBlogs.size();
    }

    /**
     * 自定义ViewHolder
     */
    public static class RecyclerViewViewHolder extends RecyclerView.ViewHolder {
        private RoundedImageView mUserhead;
        private TextView mTitle;
        private TextView mDescription;
        private TextView mTime;
        private TextView mBest;
        private TextView mComment;
        private TextView mBrowse;
        private ImageButton mMore;

        public RecyclerViewViewHolder(View view) {
            super(view);
            mUserhead = (RoundedImageView) view.findViewById(R.id.iv_userhead);
            mTitle = (TextView) view.findViewById(R.id.tv_title);
            mDescription = (TextView) view.findViewById(R.id.tv_description);
            mTime = (TextView) view.findViewById(R.id.tv_time);
            mBest = (TextView) view.findViewById(R.id.tv_best);
            mComment = (TextView) view.findViewById(R.id.tv_comment);
            mBrowse = (TextView) view.findViewById(R.id.tv_browse);
            mMore = (ImageButton) view.findViewById(R.id.ib_more);

        }
    }

    private  void imageCacheLoader(String imgUrl, Context context, ImageView imageView){
        RequestQueue  requestQueue= Volley.newRequestQueue(context);
        int maxSize=10*1024;
        final LruCache<String,Bitmap> lruCache=new LruCache<String,Bitmap>(maxSize);
        ImageLoader.ImageCache imageCache=new ImageLoader.ImageCache() {
            @Override
            public Bitmap getBitmap(String s) {
                return lruCache.get(s);
            }

            @Override
            public void putBitmap(String s, Bitmap bitmap) {
               lruCache.put(s,bitmap);
            }
        };

        ImageLoader imageLoader=new ImageLoader(requestQueue,imageCache);
        ImageLoader.ImageListener imageListener=imageLoader.getImageListener(imageView,R.drawable.photo,R.drawable.photo);
        imageLoader.get(imgUrl,imageListener);
    }
}